﻿using FitnessCenter.DataAccessLayer.Repositories.Implementations;
using FitnessCenter.DataAccessLayer.Repositories.Interfaces;
using FitnessCenter.WpfApp.Views;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Unity;
using System.Windows;

namespace FitnessCenter.WpfApp.Infrastructure
{
    class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<Shell>();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show();
        }

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();

            var moduleCatalog = (ModuleCatalog) ModuleCatalog;
            RegisterModules(moduleCatalog);
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();

            RegisterRepositories();
        }

        private void RegisterRepositories()
        {
            RegisterTypeIfMissing(typeof(IActivityLogRepository), typeof(ActivityLogRepository), true);
            RegisterTypeIfMissing(typeof(IActivityRepository), typeof(ActivityRepository), true);
            RegisterTypeIfMissing(typeof(IPersonRepository), typeof(PersonRepository), true);
            RegisterTypeIfMissing(typeof(ISubscriptionRepository), typeof(SubscriptionRepository), true);
        }

        private void RegisterModules(ModuleCatalog catalog)
        {
            catalog.AddModule(typeof(Modules.DashboardModule));
            catalog.AddModule(typeof(Modules.PersonModule));
            catalog.AddModule(typeof (Modules.LogActivityModule));
            catalog.AddModule(typeof (Modules.ActivityModule));
        }
    }
}
